package com.firword.fileconvert.entity.base;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;

/**
 * domain基础类
 *
 * @author JingYj
 * @version 1.0
 * @date 2020/8/27
 */
public class RootDomain implements Comparable<RootDomain> {
    private static final Log log = LogFactory.getLog(RootDomain.class);

    private String id; //id主键
    private Integer sort; //排序
    private Date createTime; //新建时间
    private Date updateTime; //最新更新时间

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    /**
     * 所有实体类默认排序，
     * createTime,updateTime desc,sort asc
     * 子类可以重写compareTo()方法，自定义排序规则
     *
     * @param domain
     * @return
     */
    @Override
    public int compareTo(RootDomain domain) {
        int compare = 0;
        log.debug("开始比较新建时间。");
        if (this.createTime == null && domain.createTime == null) {
            compare = 0;
        } else if (this.createTime == null) {
            compare = 1;
        } else if (domain.createTime == null) {
            compare = -1;
        } else {
            compare = this.createTime.compareTo(domain.createTime);
        }
        if (compare != 0) {
            return compare;
        }
        log.debug("开始比较最新更新时间。");
        if (this.updateTime == null && domain.updateTime == null) {
            compare = 0;
        } else if (this.updateTime == null) {
            compare = 1;
        } else if (domain.updateTime == null) {
            compare = -1;
        } else {
            compare = this.updateTime.compareTo(domain.updateTime);
        }
        if (compare != 0) {
            return compare;
        }
        log.debug("开始比较sort 。");
        if (this.sort == null && domain.sort == null) {
            compare = 0;
        } else if (this.sort == null) {
            compare = 1;
        } else if (domain.sort == null) {
            compare = -1;
        } else {
            compare = this.sort.compareTo(domain.sort);
        }
        return compare;
    }

    @Override
    public String toString() {
        return "[" + this.getClass().toString() + "],id:" + this.getId();
    }
}
